<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        table tr th,td{
            border: 1px solid black;
            text-align: center;
        }
        table{
            border: 1px;
            cell-padding:0;
            cell-spacing:0;
            width: 80%;
            margin-top: 20px;
        }
        table thead{
            background-color:#b3d7ff ;
        }
        a{
            cursor: pointer;
        }
        input{
             border-top: none;
             border-left: none;
             border-right: none;
             border-bottom: black solid 1px;
         }


    </style>
    <link rel="stylesheet" href="../css/pintuer.css">
    <link rel="stylesheet" href="../css/bootstrap.css">

    <script src="../js/jquery-3.4.1.min.js"></script>
    <script src="../js/popper.min.js"></script>
    <script src="../js/bootstrap-table1.15.3.js"></script>
    <script src="../js/bootstrap-table-zh-CN.min.js"></script>
    <script src="../js/bootstrap-min-4.3.1.js"></script>
    <script src="../js/pintuer.js"></script>
    <script src="../js/initMain.js"></script>
    <script type="text/javascript">
        function initTable(){
            $("#tableInfo").bootstrapTable({
                method:"post",
                contentType:"application/json",
                url:"/mApply/queryMApplyCheck.action",
                //设置交替行样式
                striped:true,
                //初始显示页数
                pageNum:1,
                //是否显示分页
                pagination:true,
                //设置是服务端分页还是客户端分页
                sidePagination:"server",
                //页大小
                pageSize:3,
                //分页列表
                pageList: [3,5,6,10],
                //上传到服务器的参数
                queryParams:function (data) {
                    return{
                        //计算页号，当前号
                        pageNum:(data.offset/data.limit)+1,
                        //计算页大小
                        pageSize: data.limit,
                    }
                },
                columns:[
                    {title:"点选",field:"id",align:"center",formatter:function (value,row,index) {
                            return "<input type='checkbox' id='"+value+"' productId='"+row.productId+"'/>";
                        }},
                    {title:"计划单编号",field:"applyId",align:"center"},
                    {title:"产品编号",field:"productId",align:"center"},
                    {title:"产品名称",field:"productName",align:"center"},
                    {title:"数量",field:"amount",align:"center"},
                    {title:"审核时间",field:"checkTime",align:"center"},
                ]

            })
        }
        var productId;
        $(function () {
            initTable();
            //点击预览时
            $("#readBtn").click(function () {
                var labourCostPriceSum=0;
                var moduleCostPriceSum=0;
                for(var i=0;i<$("#MDesignProcedureDetailsTable tbody tr").length;i++){
                    moduleCostPriceSum+=parseFloat($(".moduleSub").eq(i).text());
                    labourCostPriceSum+=parseFloat($(".labourSub").eq(i).text());
                }
                $("#labourCostPriceSum").text(labourCostPriceSum);
                $("#moduleCostPriceSum").text(moduleCostPriceSum);
                $("#saveMManufacture").css("display","block");
                $(this).css("display","none");

            }),
                //点击提交时
            $("#saveMManufacture").click(function () {
                var mManufacture=new Object();
                mManufacture.designer=$("#designer").val();
                mManufacture.productId=$("#productId").text();
                mManufacture.productName=$("#productName").text();
                mManufacture.amount=$("#amount").text();
                mManufacture.register=$("#register").text();
                mManufacture.remark=$("#remark").val();
                mManufacture.registerTime=$("#registerTime").text();
                mManufacture.moduleCostPriceSum=$("#moduleCostPriceSum").text();
                mManufacture.labourCostPriceSum=$("#labourCostPriceSum").text();
                //创建生产工序表(M_PROCEDURE)实体类数组
                var mProcedureArrs=[];
                for(var i=0;i<$("#MDesignProcedureDetailsTable  tbody tr").length;i++){

                    var mProcedure=new Object();
                    //暂时将id设置为MDesignProcedureDetailsId
                    mProcedure.id=$(".MDesignProcedureDetailsId").eq(i).text();
                    // alert(mProcedure.id);
                    mProcedure.detailsNumber=i+1;
                    mProcedure.procedureId=$(".procedureId").eq(i).text();
                    mProcedure.procedureName=$(".procedureName").eq(i).text();
                    mProcedure.labourHourAmount=$(".labourHourAmount").eq(i).text();
                    mProcedure.subtotal=$(".labourSub").eq(i).text();
                    mProcedure.moduleSubtotal=$(".moduleSub").eq(i).text();
                    mProcedure.costPrice=$(".costPrice").eq(i).text();
                    mProcedure.procedureFinishTag="G004-0";
                    mProcedure.procedureTransferTag="G005-0";

                    mProcedureArrs.push(mProcedure);

                }

                $.ajax({
                    url:"/mManufacture/addMManufacture.action",
                    type: "post",
                    data:"mManufactureStr="+JSON.stringify(mManufacture)+"&mApplyIdsStr="+JSON.stringify(arrIds)+"&mProcedureArrs="+JSON.stringify(mProcedureArrs),
                    success:function (data) {
                        alert("制定成功！");
                        $("#addModel").modal("hide");
                        $("#tableInfo").bootstrapTable("refresh");
                    }
                })
            })
            //点击生成派工单
            var arrIds=[];
            $("#addMManufacture").click(function () {
                $("#saveMManufacture").css("display","none");
                $("#readBtn").css("display","block");

                var length=$("input[type='checkbox']:checked").length;
                // alert(length);
                if(length<=0){
                    alert("至少选择一个产品生成派工单！")
                }else{
                    var b=true;
                    for(var i=0;i<length-1;i++){
                        if($("input[type='checkbox']:checked").eq(i).attr("productId")!=$("input[type='checkbox']:checked").eq(i+1).attr("productId")){
                            b=false;
                        }
                    }
                    if(b==false){
                        alert("请选择产品编号一致的产品制定生产派工单！");
                    }else{

                        for(var i=0;i<length;i++){
                            var mApplyId=$("input[type='checkbox']:checked").eq(i).attr("id");
                            arrIds.push(mApplyId);
                        }
                        productId=$("input[type='checkbox']:checked").eq(0).attr("productId");
                        // 保证物料设计单、工序设计单、工序物料设计单都已设计完毕并通过审核,并查询mApply
                        $.ajax({
                            url: "/mDesignProcedure/queryMDesignProcedureIfCheckGreen.action",
                            data:"productId="+productId+"&mApplyIds="+JSON.stringify(arrIds),
                            type:"post",
                            success:function (data) {
                                if(data.length==0||data==''||data=="null"){
                                    alert("请先完成工序设计单和工序物料设计单！")
                                }else{
                                    $("#addModel").modal("show");
                                    var amount=0;
                                    for(var i=0;i<data.length;i++){
                                        var mApply=data[i];
                                        $("#productId").text(mApply.productId);
                                        $("#productName").text(mApply.productName);
                                        amount+=parseInt(mApply.amount);
                                    }
                                    $("#amount").text(amount);
                                    $("#register").text(localStorage.getItem("userName"));
                                    var date=new Date();
                                    $("#registerTime").text(date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+"  "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());

                                    $("#MDesignProcedureDetailsTable").bootstrapTable('destroy');
                                    loadMDesignProcedureDetails();

                                }
                            }
                        })

                    }
                }
            })
        })
        var MDesignProcedureDetailsId;
        //点击查看物料时
        function queryModule(MDesignProcedureDetails) {
            MDesignProcedureDetailsId=MDesignProcedureDetails.id;
            // alert(MDesignProcedureDetails.id);
            $("#addDesignModel").modal("show");
            $("#MDesignModuleTable").bootstrapTable('destroy');
            initMDesignModule();
            $("#procedureId").text(MDesignProcedureDetails.procedureId);
            $("#procedureName").text(MDesignProcedureDetails.procedureName);
            $("#moduleSubtotal").text(MDesignProcedureDetails.moduleSubtotal*parseInt($("#amount").text()));
        }
        //通过产品编号查询产品工序物料明细
        function loadMDesignProcedureDetails() {
            $("#MDesignProcedureDetailsTable").bootstrapTable({
                method:"post",
                contentType:"application/json",
                url:"/mDesignProcedureDetails/queryMDesignProcedureDetailsByProductId",
                //设置交替行样式
                striped:true,
                //初始显示页数
                pageNum:1,
                //是否显示分页
                pagination:false,
                //设置是服务端分页还是客户端分页
                sidePagination:"server",
                //页大小
                pageSize:3,
                //分页列表
                pageList: [3,5,6,10],
                //上传到服务器的参数
                queryParams:function (data) {
                    return{
                        //计算页号，当前号
                        pageNum:(data.offset/data.limit)+1,
                        //计算页大小
                        pageSize: data.limit,

                        productId:productId
                    }
                },

                columns:[
                    {title:"ID",field:"id",align:"center",visible:"invisible",formatter:function (value) {
                            return "<span class='MDesignProcedureDetailsId'>"+value+"</span>"
                        }},
                    {title:"序号",field:"detailsNumber",align:"center",formatter:function (value) {
                            return "<span>"+value+"</span>"
                        }},
                    {title:"工序名称",field:"procedureName",align:"center",formatter:function (value,row,index) {
                            return "<span class='procedureName'>"+value+"</span>"
                        }},
                    {title:"工序编号",field:"procedureId",align:"center",formatter:function (value,row,index) {
                            return "<span class='procedureId'>"+value+"</span>"
                        }},
                    {title:"工时数(小时)",field:"labourHourAmount",align:"center",formatter:function (value,row,index) {
                        return "<span class='labourHourAmount'>"+value*parseInt($("#amount").text())+"</span>"
                    }},
                    {title:"单位工时成本",field:"costPrice",align:"center",formatter:function (value,row,index) {
                        return "<span class='costPrice'>"+value+"</span>"
                    }},
                    {title:"工时成本小计(元)",field:"subtotal",align:"center",formatter:function (value,row,index) {
                            return "<span class='labourSub'>"+value*parseInt($("#amount").text())+"</span>";
                        }},
                    {title:"物料成本小计(元)",field:"moduleSubtotal",align:"center",formatter:function (value,row,index) {
                            return "<span class='moduleSub'>"+value*parseInt($("#amount").text())+"</span>";
                        }},
                    {title:"查看物料",field:"id",align:"center",formatter:function (value,row,index) {
                            return "<a href='#' onclick='queryModule("+JSON.stringify(row)+")'>查看物料</a>"
                        }},
                ]
            })
        }
        function initMDesignModule() {
            $("#MDesignModuleTable").bootstrapTable({
                method:"post",
                contentType:"application/json",
                url:"/mDesignProcedureModule/queryMDesignProcedureModuleByPId1.action",
                //设置交替行样式
                striped:true,
                //初始显示页数
                pageNum:1,
                //是否显示分页
                pagination:true,
                //设置是服务端分页还是客户端分页
                sidePagination:"server",
                //页大小
                pageSize:3,
                //分页列表
                pageList: [3,5,6,10],
                //上传到服务器的参数
                queryParams:function (data) {
                    return{
                        //计算页号，当前号
                        pageNum:(data.offset/data.limit)+1,
                        //计算页大小
                        pageSize: data.limit,

                        pId:MDesignProcedureDetailsId
                    }
                },

                columns:[

                    {title:"序号",field:"detailsNumber",align:"center"},
                    {title:"物料名称",field:"productName",align:"center"},
                    {title:"物料编号",field:"productId",align:"center"},
                    {title:"本工序数量",field:"amount",align:"center",formatter:function (value,row,index) {
                        return value*parseInt($("#amount").text());
                    }},
                    {title:"单位",field:"amountUnit",align:"center"},
                    {title:"单价",field:"costPrice",align:"center"},
                    {title:"小计",field:"subtotal",align:"center",formatter:function (value,row,index) {
                            return value*parseInt($("#amount").text());
                        }}
                ]
            })
        }
    </script>
</head>
<body>
    <div>
        <div class="container">
            <div class="row">
                <div class="col-7" ></div>
                <div class="col-3"></div>
                <div class="col-2" style="margin-right: 0px">
                    <button class="btn btn-primary" id="addMManufacture" >生成派工单</button>
                </div>
            </div>
        </div>
        <table id="tableInfo">

        </table>
    </div>
    <div class="modal  fade" id="addModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="width: 90%">
            <div class="modal-content" style="border: 1px solid black;">
                <div class="modal-header">
                    <div class="container">
                        <div class="row">
                            <div class="col-7" style="text-align: right">
                                <h5 class="modal-title" id="info" >
                                    生产派工单
                                </h5>
                            </div>
                            <div class="col-3"></div>
                            <div class="col-2" style="position: absolute;right: 30px">

                                <button class="btn btn-primary" id="readBtn" >预览</button>
                                <button class="btn btn-primary" style="display: none" id="saveMManufacture" >提交</button>
                            </div>
                        </div>
                    </div>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-right: 0px">

                    </button>
                </div>
                <div class="modal-body">
                    <form id="addProcedureForm">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-2" style="text-align: right">
                                    <span>工单制定人：</span>
                                </div>
                                <div class="col-4">
                                    <input id="designer"><br>
                                </div>
                                <div class="col-2" style="text-align:right">
                                    数量：
                                </div>
                                <div class="col-4">
                                    <span id="amount"></span>
                                </div>

                            </div>
                        </div><br>

                        <div class="container">
                            <div class="row">
                                <div class="col-2" style="text-align:right">
                                    产品名称：
                                </div>
                                <div class="col-4">
                                    <span id="productName">笔记本</span>
                                </div>
                                <div class="col-2" style="text-align:right">
                                    产品编号：
                                </div>
                                <div class="col-4">
                                    <span id="productId" >20001002192001</span>
                                </div>
                            </div>
                        </div><br>
                        <div class="container">
                            <div class="row">
                                <div class="col-2" style="text-align:right">
                                    物料总成本：
                                </div>
                                <div class="col-4">
                                    <span id="moduleCostPriceSum"></span>
                                </div>
                                <div class="col-2" style="text-align:right">
                                    工时总成本：
                                </div>
                                <div class="col-4">
                                    <span id="labourCostPriceSum"></span>
                                </div>
                            </div>
                        </div><br>


                        <table id="MDesignProcedureDetailsTable"  style="margin-left: 20px">

                        </table><br>

                        <div class="container">
                            <div class="row">
                                <div class="col-2" style="text-align:right">
                                    登记人：
                                </div>
                                <div class="col-4">
                                    <span name="register" id="register"></span>
                                </div>
                                <div class="col-2" style="text-align:right">
                                    登记时间：
                                </div>
                                <div class="col-4">
                                    <span  name="registerTime" id="registerTime"></span>
                                </div>
                            </div>
                        </div><br>
                        <span style="margin-left: 55px">备注：</span>
                        <textarea style="width: 550px;margin-left: 150px" class="form-control" id="procedureDescribe">

                        </textarea>

                    </form>
                </div>

            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>


<!--    设计工序物料-->
    <div class="modal  fade" id="addDesignModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="width: 90%;height: 500px">
            <div class="modal-content" style="border: 1px solid black;">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-right: 0px">

                    </button>
                </div>
                <div style="width: 80%;height: 400px;border: 2px solid gray;margin: 0 auto;margin-top: 10px">
                    <h3 align="center" style="margin-top: 30px">工序物料单</h3>
                    <div class="container-fluid">
                        <form id="putRegister">
                            <div class="row" style="margin-top: 30px;">
                                <div class="col-3" style="text-align:right">
                                    设计单编号：
                                </div>
                                <div class="col-4">
                                    <span id="procedureId"></span>
                                </div>
                                <div class="col-2" style="text-align:right">
                                    工序名称：
                                </div>
                                <div class="col-3">
                                    <span id="procedureName"></span>
                                </div>

                            </div>
                            <div class="row" style="margin-top: 30px;">
                                <div class="col-3" style="text-align:right">
                                    物料总成本：
                                </div>
                                <div class="col-4">
                                    <span id="moduleSubtotal"></span>
                                </div>

                            </div>

                            <div>
                                <table id="MDesignModuleTable" width="80%" style="margin-top: 20px;" class="table table-sm">

                                </table>
                            </div>


                        </form>
                    </div>
                </div>

            </div>
        </div>
    </div>
</body>

</html>